package com.topc.core.sevice.impl;


import cn.hutool.core.util.ObjectUtil;
import com.topc.core.sevice.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;



public class RedisServiceImpl implements RedisService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public void set(String key, Object value, long time) {
        redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
    }

    @Override
    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    @Override
    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    @Override
    public int getCount(String key) {
        Object o = redisTemplate.opsForValue().get(key);
        if (ObjectUtil.isEmpty(o)) {
            this.set(key,0);
            return 0;
        }
        return (int) o;
    }

    @Override
    public Boolean del(String key) {
        return redisTemplate.delete(key);
    }

    @Override
    public Long del(List<String> keys) {
        return redisTemplate.delete(keys);
    }

    @Override
    public Boolean expire(String key, long time) {
        return redisTemplate.expire(key, time, TimeUnit.SECONDS);
    }

    @Override
    public Long getExpire(String key) {
        return redisTemplate.getExpire(key, TimeUnit.SECONDS);
    }

    @Override
    public Boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    @Override
    public Long incr(String key, long delta) {
        return redisTemplate.opsForValue().increment(key, delta);
    }

    @Override
    public Long decr(String key, long delta) {
        return redisTemplate.opsForValue().increment(key, -delta);
    }

    @Override
    public Object hGet(String key, String hashKey) {
        return redisTemplate.opsForHash().get(key, hashKey);
    }

    @Override
    public Integer hGetCount(String key, String hashKey) {
        Object o = redisTemplate.opsForHash().get(key, hashKey);
        if (ObjectUtil.isEmpty(o)) {
            this.hSet(key,hashKey,0);
            return 0;
        }
        return (Integer) o;
    }

    @Override
    public Boolean hSet(String key, String hashKey, Object value, long time) {
        redisTemplate.opsForHash().put(key, hashKey, value);
        return expire(key, time);
    }

    @Override
    public void hSet(String key, String hashKey, Object value) {
        redisTemplate.opsForHash().put(key, hashKey, value);
    }

    @Override
    public Map<Object, Object> hGetAll(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    @Override
    public Boolean hSetAll(String key, Map<String, Object> map, long time) {
        redisTemplate.opsForHash().putAll(key, map);
        return expire(key, time);
    }

    @Override
    public void hSetAll(String key, Map<String, ? > map) {
        redisTemplate.opsForHash().putAll(key, map);
    }

    @Override
    public void hDel(String key, Object... hashKey) {
        redisTemplate.opsForHash().delete(key, hashKey);
    }

    @Override
    public Boolean hHasKey(String key, String hashKey) {
        return redisTemplate.opsForHash().hasKey(key, hashKey);
    }

    @Override
    public Long hIncr(String key, String hashKey, Long delta) {
        return redisTemplate.opsForHash().increment(key, hashKey, delta);
    }

    @Override
    public Long hDecr(String key, String hashKey, Long delta) {
        return redisTemplate.opsForHash().increment(key, hashKey, -delta);
    }

    @Override
    public Set<Object> sMembers(String key) {
        return redisTemplate.opsForSet().members(key);
    }

    @Override
    public Long sAdd(String key, Object... values) {
        return redisTemplate.opsForSet().add(key, values);
    }

    @Override
    public Long sAdd(long time,String key, Object... values) {
        Long count = redisTemplate.opsForSet().add(key, values);
        expire(key, time);
        return count;
    }

    @Override
    public Boolean sIsMember(String key, Object value) {
        return redisTemplate.opsForSet().isMember(key, value);
    }

    @Override
    public Long sSize(String key) {
        return redisTemplate.opsForSet().size(key);
    }

    @Override
    public Long sRemove(String key, Object... values) {
        return redisTemplate.opsForSet().remove(key, values);
    }

    @Override
    public List<Object> lRange(String key, long start, long end) {
        return redisTemplate.opsForList().range(key, start, end);
    }


    @Override
    public Long lSize(String key) {
        return redisTemplate.opsForList().size(key);
    }

    @Override
    public Object lIndex(String key, long index) {
        return redisTemplate.opsForList().index(key, index);
    }

    @Override
    public Long lPush(String key, Object value) {
        return redisTemplate.opsForList().rightPush(key, value);
    }

    @Override
    public Long lPush(String key, Object value, long time) {
        Long index = redisTemplate.opsForList().rightPush(key, value);
        expire(key, time);
        return index;
    }

    @Override
    public Long lrPush(String key, Object value) {
        return redisTemplate.opsForList().leftPush(key, value);
    }

    @Override
    public Long lrPush(String key, Object value, long time) {
        Long index = redisTemplate.opsForList().leftPush(key, value);
        expire(key, time);
        return index;
    }


    @Override
    public Long lPushAll(String key, Object... values) {
        return redisTemplate.opsForList().rightPushAll(key, values);
    }

    @Override
    public Long lPushAll(String key, Long time, Object... values) {
        Long count = redisTemplate.opsForList().rightPushAll(key, values);
        expire(key, time);
        return count;
    }

    @Override
    public Long lRemove(String key, long count, Object value) {
        return redisTemplate.opsForList().remove(key, count, value);
    }

    /**
     * 向 ZSet 中添加元素
     *
     * @param key
     * @param value
     * @param score
     */
    @Override
    public Boolean zsAdd(String key, String value, double score) {
        return redisTemplate.opsForZSet().add(key, value, score);
    }

    /**
     * 获取 ZSet 的元素个数
     *
     * @param key
     */
    @Override
    public Long zsCountAll(String key) {
        return redisTemplate.opsForZSet().zCard(key);
    }

    /**
     * 获取 ZSet 指定元素的分数
     *
     * @param key
     * @param value
     */
    @Override
    public Double zsScore(String key, String value) {
        return redisTemplate.opsForZSet().score(key, value);
    }

    /**
     * 获取ZSet指定范围内的元素
     *
     * @param key
     * @param start
     * @param end
     */
    @Override
    public Set<Object> zsRange(String key, long start, long end) {
        return redisTemplate.opsForZSet().range(key, start, end);
    }

    /**
     * 获取ZSet指定范围内的元素及其分数
     *
     * @param key
     * @param start
     * @param end
     */
    @Override
    public Set<ZSetOperations.TypedTuple<Object>> zsRangeWithScores(String key, long start, long end) {
        return redisTemplate.opsForZSet().rangeWithScores(key, start, end);
    }

    /**
     * 获取ZSet指定范围内的元素及其分数(倒序)
     *
     * @param key
     * @param start
     * @param end
     */
    @Override
    public Set<ZSetOperations.TypedTuple<Object>> zsReverseRangeWithScores(String key, long start, long end) {
        return redisTemplate.opsForZSet().reverseRangeWithScores(key, start, end);
    }

    /**
     * 倒序获取所有元素
     *
     */
    @Override
    public Set<ZSetOperations.TypedTuple<Object>> zsGetAllReverse(String key) {
        return redisTemplate.opsForZSet().reverseRangeWithScores(key, 0,-1);
    }

    /**
     * 获取ZSet指定分数范围内的元素（倒序）
     *
     * @param key
     * @param min
     * @param max
     */
    @Override
    public Set<Object> zsReverseRangeByScore(String key, double min, double max) {
        return redisTemplate.opsForZSet().reverseRangeByScore(key, min, max);
    }

    /**
     * 获取ZSet指定元素的排名（从小到大）
     *
     * @param key
     * @param value
     */
    @Override
    public Long zsRank(String key, String value) {
        return redisTemplate.opsForZSet().rank(key, value);
    }

    /**
     * 获取ZSet指定元素的排名（从大到小）
     *
     * @param key
     * @param value
     */
    @Override
    public Long zsReverseRank(String key, String value) {
        return redisTemplate.opsForZSet().reverseRank(key, value);
    }

    /**
     * 增加ZSet指定元素的分数
     *
     * @param key
     * @param value
     * @param delta
     */
    @Override
    public Double zsIncrementScore(String key, String value, long delta) {
        return redisTemplate.opsForZSet().incrementScore(key, value, delta);
    }

    /**
     * 获取ZSet指定分数范围内的元素个数
     *
     * @param key
     * @param min
     * @param max
     */
    @Override
    public Long zsCount(String key, double min, double max) {
        return redisTemplate.opsForZSet().count(key, min, max);
    }

    /**
     * 删除ZSet中的指定元素
     *
     * @param key
     * @param values
     *
     * @Return 删除元素的数量
     */
    @Override
    public Long zsRemove(String key, Object... values) {
        return redisTemplate.opsForZSet().remove(key, values);
    }

    /**
     * 删除ZSet中指定排名范围内的元素
     *
     * @param key
     * @param start
     * @param end
     */
    @Override
    public Long zsRemoveRange(String key, long start, long end) {
        return redisTemplate.opsForZSet().removeRange(key, start, end);
    }
}
